﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.Script.Services;
using System.Web.Services;
using API.DataAccessObject;
using API.EntityObject;
using DataAccessObject;
using EntityObject;
using ThangNM_LIB.EO;
using ThangNM_LIB.FUNC;

namespace API
{
    /// <summary>
    /// Summary description for API_Public
    /// </summary>
    [WebService(Namespace = "http://api.nguyenmanhthang.net/", Description = "Nguyễn Mạnh Thắng - API")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

    [System.Web.Script.Services.ScriptService]
    public class API_Public : System.Web.Services.WebService
    {
        public String sUsername { get; set; }

        [WebMethod(Description = "Tra cứu đáp án giải mã đường đua ZingMe - http://me.zing.vn/giaimaduongdua/cd", EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public List<RacingWordEO> Racing_Word(String sQuery, String sToken)
        {
            List<RacingWordEO> lstRet = new List<RacingWordEO>();
            LogEO objLog;
            try
            {
                if (Security.WebServices_CheckTokenKey(sToken))
                {
                    String strInput = StringEx.Remove_Unicode_Character(StringEx.TrimEx(sQuery));
                    lstRet = (new RacingWordDAO()).SelectList_ByKey(strInput);
                    if (lstRet.Count > 0)
                        objLog = new LogEO(String.Format(MessagesEx.ATTACH_TWO_INFO, sQuery, lstRet.Count.ToString()), ConstEx.Log_iType.GMDD_ZINGME_OK);
                    else
                        objLog = new LogEO(String.Format(MessagesEx.ATTACH_TWO_INFO, sQuery, lstRet.Count.ToString()), ConstEx.Log_iType.GMDD_ZINGME_NOT_FOUND);
                }
                else
                    objLog = new LogEO(sQuery, ConstEx.Log_iType.KARAOKE_ACCESS_DENIAL);
            }
            catch (Exception ex)
            {
                objLog = new LogEO(String.Format(MessagesEx.ATTACH_TWO_INFO, sQuery, ex.ToString()), ConstEx.Log_iType.GMDD_ZINGME_ERROR);
            }
            (new Logs()).WriteLog_To_DB(objLog, false);
            return lstRet;
        }

        [WebMethod(Description = "Tra cứu mã số, bài hát Karaoke", EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public List<KaraokeEO> Karaoke_Search(String sQuery, String sToken)
        {
            List<KaraokeEO> lstRet = new List<KaraokeEO>();
            LogEO objLog;
            try
            {
                if (Security.WebServices_CheckTokenKey(sToken))
                {
                    String strInput = StringEx.Remove_Unicode_Character(StringEx.TrimEx(sQuery));
                    lstRet = (new KaraokeDAO()).SelectList_ByKey(strInput);
                    if (lstRet.Count > 0)
                        objLog = new LogEO(String.Format(MessagesEx.ATTACH_TWO_INFO, sQuery, lstRet.Count.ToString()), ConstEx.Log_iType.KARAOKE_OK);
                    else
                        objLog = new LogEO(String.Format(MessagesEx.ATTACH_TWO_INFO, sQuery, lstRet.Count.ToString()), ConstEx.Log_iType.KARAOKE_NOT_FOUND);
                }
                else
                    objLog = new LogEO(sQuery, ConstEx.Log_iType.KARAOKE_ACCESS_DENIAL);
            }
            catch (Exception ex)
            {
                objLog = new LogEO(String.Format(MessagesEx.ATTACH_TWO_INFO, sQuery, ex.ToString()), ConstEx.Log_iType.KARAOKE_ERROR);
            }
            (new Logs()).WriteLog_To_DB(objLog, false);
            return lstRet;
        }



        [WebMethod(Description = "Ghi lịch sử", EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public Boolean Write_Log(String sContent, Int16 iType, String sToken)
        {
            if (Security.WebServices_CheckTokenKey(sToken))
            {
                LogEO objLog = new LogEO(sContent, iType);
                return (new Logs()).WriteLog_To_DB(objLog, false);
            }
            return false;
        }

        [WebMethod(Description = "Ghi lịch sử", EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public Boolean Write_Log_Public(String sContent, Int16 iType)
        {
            LogEO objLog = new LogEO(sContent, iType);
            return (new Logs()).WriteLog_To_DB(objLog, false);
        }




        [WebMethod(Description = "Cập nhật phần mềm", EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public String Update_Version_Software(Int32 PK_iAppID, Int32 PK_iVerID)
        {
            String strRet;
            VersionEO objRet = new VersionEO();
            try
            {
                objRet = (new VersionDAO()).SelectItem(PK_iAppID, PK_iVerID);
                if (objRet != null)
                {
                    if (objRet.bFileUpdate == null || objRet.bFileUpdate.Length <= 0)
                        objRet.bFileUpdate = File.ReadAllBytes(@"E:\SETUP\bluescreenview.zip");
                    else if (!String.IsNullOrEmpty(objRet.sUrl))
                    {
                        if (StringEx.CompareEx(objRet.sUrl, "http://") || StringEx.CompareEx(objRet.sUrl, "https://"))
                            objRet.bFileUpdate = File.ReadAllBytes(@"E:\SETUP\bluescreenview.zip");
                        else if (StringEx.CompareEx(objRet.sUrl, "ftp://") || StringEx.CompareEx(objRet.sUrl, "ftps://"))
                            objRet.bFileUpdate = File.ReadAllBytes(@"E:\SETUP\bluescreenview.zip");
                        else
                            objRet.bFileUpdate = Folder_File.ReadAllBytes(objRet.sUrl);
                    }
                }
            }
            catch
            {
            }
            strRet = Convert.ToBase64String(Encoding.UTF8.GetBytes(XmlEx.Object2Xml<VersionEO>(objRet, String.Empty)));
            return strRet;
        }

        [WebMethod(Description = "Đồng bộ phiên bản", EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public Boolean Sync_Version_Software(Int32 PK_iAppID)
        {
            return false;
        }

        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string simplestMethod(String abc)
        {
            if (!String.IsNullOrEmpty(sUsername) && sUsername.ToLower().Equals("thangnm11"))
                return "Simplest method returned [" + abc + "]";
            else
                return "401 Access Denied";
        }
    }
}
